package com.wbf.demo.controller;

import com.wbf.demo.domain.Subject;
import com.wbf.demo.mapper.UserMapper;
import com.wbf.demo.mobile.User;
import com.wbf.demo.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: security_demo
 * @description: 测试控制器
 * @author: guangdao
 * @create: 2022-04-14 17:13
 **/
@RestController
public class TestController {
    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 包含获取登录用户信息/@Cacheable/@Cacheable失效时间
     * @param token
     * @param userId
     * @return
     */
    @PreAuthorize("hasAnyRole('user')")
    @GetMapping("/test")
    public User getUser(Authentication token,int userId){
        // 可成功返回
        Subject subject = (Subject) token.getPrincipal();
        System.out.println(subject);
        return userService.getUserInfo(userId);
    }

    @PreAuthorize("hasAnyRole('goods')")
    @GetMapping("/goods")
    public String getGoods(){
        // 无权限返回403
        return "哈哈哈哈 返回商品";
    }

    @GetMapping("/redis")
    public Object setRedisValue(String key,String val){
        redisTemplate.opsForValue().set(key,val);
        return redisTemplate.opsForValue().get(key);
    }
}
